import Router from 'vue-router'
import bussionessRoutes from './bussiness/routes'
import systemRoutes from './system/routes'
import userData from '../localData/userData'
//合并三类路由
var routes = bussionessRoutes.concat(systemRoutes);

export default (store) => {
  var routerObj = new Router({
    routes,
    linkActiveClass: 'active-link'
  })

  //初始化仿微信侧滑动画处理
  const history    = window.sessionStorage;
  let historyCount = history.getItem('count') * 1;

  routerObj.beforeEach(function (to, from, next) {

    //设置标题
    if(to.name != undefined && to.name.length > 0){
      document.title = to.name
    }

    const toIndex = history.getItem(to.path);
    const fromIndex = history.getItem(from.path);

    if (toIndex) {
      if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
        store.commit('AppModule/abc', {direction: 'forward'})
      } else {
        store.commit('AppModule/abc', {direction: 'reverse'})
      }
    } else {
      ++historyCount;
      history.setItem('count', historyCount);
      to.path !== '/' && history.setItem(to.path, historyCount);
      store.commit('AppModule/abc', {direction: 'forward'})
    }
    if (/\/http/.test(to.path)) {
      let url = to.path.split('http')[1];
      window.location.href = `http${url}`
    } else {
      next()
    }

  });

  routerObj.beforeEach((to, from, next) => {  

    var goRouterUrl = to.path

    if(goRouterUrl == '/home' || goRouterUrl == '/communicationList' || goRouterUrl == '/agent' || goRouterUrl == '/user'){
      routerObj.app.$options.store.commit('AppModule/showAppTab')
    }else{
      routerObj.app.$options.store.commit('AppModule/hiddenAppTab')
    }
    next()

  })
  
  routerObj.beforeEach((to, from, next) => {  

    var goRouterUrl = to.path

    //判断是否需要登录
    if(goRouterUrl != '/login'){

//    if(goRouterUrl == '/agentInformationInput' || goRouterUrl == '/salesInformationInput'){
//       next();
//    }else{

        if(!userData.isLogin()){
           routerObj.push('/login');
        }
//    }
  
      next();
  
    }else{

      next();

    }

  })


  return routerObj
}
